package com.download.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.download.download.MulThreadDownload;
import com.download.entity.DownloadInfo;
import com.download.exception.DownloadException;

public class DownloadHelper {

    // 下载文件默认保存路径
    public static final String FILE_PATH = "C:/temp";
    // 默认分块数、线程数
    public static final int SPLITTER_NUM = 3;
    
    public static MulThreadDownload mulThreadDownload;
    
    private DownloadHelper(){
    }
    
    public static DownloadInfo createDownloadInfo(String url, String fileName, String filePath, int splitter){
        DownloadInfo downloadInfo = new DownloadInfo();
        downloadInfo.setUrl(url);
        downloadInfo.setFileName((fileName == null || "".equals(fileName)) ? getFileNameByUrl(url) : fileName);
        downloadInfo.setFilePath((filePath == null || "".equals(filePath)) ? FILE_PATH : filePath);
        downloadInfo.setSplitter((splitter < 1) ? SPLITTER_NUM : splitter);
        return downloadInfo;
    }
    
    public static void executeDownload(DownloadInfo downloadInfo){
        mulThreadDownload = new MulThreadDownload(downloadInfo);
        new Thread(mulThreadDownload).start();
    }
    
    public static float getDownloadProcess(){
        return mulThreadDownload.getDownloadProgress();
    }

    public static String getFileNameByUrl(String url) {
        String filename = "";
        try {
        //从http响应种获取文件名称
            String contentDisposition = HttpUtil.getHttpConnection(url).getHeaderField("Content-Disposition");
            if(contentDisposition != null && !"".equals(contentDisposition)){
                filename = processorFileName(contentDisposition.split("filename=")[1]);
                filename = new String(filename.getBytes("iso8859-1"), "gbk");
            //    filename = new String(new String(filename.getBytes("iso8859-1"), "gbk").getBytes("gbk"), "utf8");
            } else if ("".equals(filename)) {
                if (url.indexOf("?") != -1) {
                    filename = url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));    
                }
                else {
                    filename = url.substring(url.lastIndexOf("/") + 1, url.length());    
                }
            }
        } catch (Exception e) {
            throw new DownloadException("DownloadHelper.getFileNameByUrl()", e);
        }
        return filename;
    }

    public static boolean isUrl (String inputStr) {
        if(inputStr == null) {
            return false;
        }
        String regEx = "(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?";
        Pattern p = Pattern.compile(regEx);
        Matcher matcher = p.matcher(inputStr);
        return matcher.matches();
    }
    
    public static String processorFileName(String fileName){
        Pattern p=Pattern.compile("[\"\\?!:'<>]");//增加对应的标点
        Matcher m=p.matcher(fileName);
        return  m.replaceAll(""); //把英文标点符号替换成空，即去掉英文标点符号
    }

}
